package com.xeware.chatter.view;

import com.xeware.chatter.R;
import com.xeware.chatter.service.ConnectionManager;
import com.xeware.chatter.util.StaticInformation;
import com.sforce.soap.partner.PartnerConnection;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
//import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class LoginController extends Activity {
	//private static final String TAG = "Chatter Start Page";
	public static final String SETTINGS = "ChatterClient";
	private static Button loginButton;
	private static EditText userId;
	private static EditText userPw;
	private static EditText userTk;
	public PartnerConnection connection; 
	public static String muserId = "";
	public static String muserPw = "";
	public static String muserTk = "";
	
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_menu);               		
        initFormFunction();		
    }

	private void initFormFunction() {
    	userId = (EditText) findViewById(R.id.salesforce_user_id);
		userPw = (EditText) findViewById(R.id.salesforce_user_password);
		userTk = (EditText) findViewById(R.id.salesforce_user_token);
		
        SharedPreferences settings = getSharedPreferences(SETTINGS, 0);
        LoginController.muserId = settings.getString("userId", "");
        LoginController.muserPw = settings.getString("userPw", "");
        LoginController.muserTk = settings.getString("userTk", "");


        userId.setText(LoginController.muserId);
        userPw.setText(LoginController.muserPw);
        userTk.setText(LoginController.muserTk);
		
		//userId.setText("xyz");
		//userPw.setText("xyz");
		//userTk.setText("xyz");
		
		loginButton = (Button) findViewById(R.id.btnLogin);	   	
    	loginButton.setOnClickListener(new OnClickListener() {
    		@Override
    	    public void onClick(View v) {
    			new LoginHandler().execute();
    	    }
       });
	}
	
	@Override
    protected void onStop() {
    	super.onStop();
    	this.commitSettings();
    }

	
	private class LoginHandler extends AsyncTask<Void, Void, Void> {
		private ProgressDialog pd = new ProgressDialog(LoginController.this);
		
		protected void onPreExecute() {  
			pd.setMessage("Logging In..."); 
			pd.setIndeterminate(true);
			pd.setCancelable(false);
			pd.show();  
		}
		 
		@Override
		protected Void doInBackground(Void... unused) {
			StaticInformation.USER_NAME = userId.getText().toString();
			StaticInformation.USER_PW = userPw.getText().toString();
			StaticInformation.USER_TOKEN = userTk.getText().toString();			
			connection = ConnectionManager.getConnectionManager().getConnection();
			return null;
		}

	    protected void onPostExecute(Void unused) {
	    	goToFeedList(); 
	    	pd.dismiss();
	    }
	}
	
	protected void commitSettings() {
        SharedPreferences settings = getSharedPreferences(SETTINGS, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("userId", StaticInformation.USER_NAME);
        editor.putString("userPw", StaticInformation.USER_PW);
        editor.putString("userTk", StaticInformation.USER_TOKEN);
        editor.commit();		
	}
	
	private void goToFeedList() {
		Intent myIntent = new Intent(LoginController.this, FeedListController.class);
		//Intent myIntent = new Intent(LoginController.this, FeedExpandableListController.class);
		try {
			startActivity(myIntent);
		} catch (Exception e) {
			//Log.i(TAG, "Unable to launch activity...");
		}
	}
}
